#
# @lc app=leetcode.cn id=234 lang=python3
#
# [234] 回文链表
#

# @lc code=start
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def isPalindrome(self, head: ListNode) -> bool:
        # ????????????
        if not head: return True
        # ?????????
        slow, fast, pre= head, head, None
        while fast:
            slow = slow.next
            fast = fast.next.next if fast.next else fast.next
        # ???????????
        while slow:
            slow.next, slow, pre = pre, slow.next, slow
        # ???????
        while head and pre:
            if head.val != pre.val: return False
            head = head.next
            pre = pre.next
        return True

# @
# lc code=end

